package cc.wfu.domain.auth.service;

import cc.wfu.types.enums.Constants;
import cc.wfu.types.exception.UserServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * @Author: jia-qiang ljq1024.cc
 * @desc: 用户权限校验 - 验证码等
 * @Date: 2024-05-16-11:25
 */
@Slf4j
@Service
public class AuthService extends AuthAbstractService {

    @Resource
    private PasswordEncoder passwordEncoder;

    /**
     * 校验【密码】是否正确
     * @param encodePassword 数据库加密密码
     * @param verifyPassword 待验证密码
     */
    @Override
    protected void verifyPassword(String encodePassword, String verifyPassword) {
        if (!passwordEncoder.matches(verifyPassword, encodePassword)) {
            throw new UserServiceException(Constants.ResponseError.ILLEGAL_USER_PASSWORD_ERROR.getInfo());
        }
    }

    /**
     * 发送验证码
     * @param mail 邮箱
     * @param code 验证码
     */
    @Override
    protected void sendEmailCode(String mail, String code) {
        emailService.sendMail(Constants.LOGIN_CODE_TITLE, "您的登录验证码为：" + code + " 不区分大小写，有效期为5分钟，请勿转发！", mail);
    }

    @Override
    public String encode(String newPassword) {
        return passwordEncoder.encode(newPassword);
    }
}
